﻿Imports System.Runtime.InteropServices
Imports System.Windows
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks

'Dim newmarg As New MARGINS With {.Top = -1, .bottom = -1, .right = -1, .left = -1}
'GlassHelper.ExtendGlassFrame(Me, newmarg)

<StructLayout(LayoutKind.Sequential)>
Public Structure MARGINS
    Public Sub MARGINS(Of Thickness)(T)
        left = CInt(T.left)
        right = CInt(T.right)
        Top = CInt(T.top)
        bottom = CInt(T.bottom)
    End Sub

    Public left As Integer
    Public right As Integer
    Public Top As Integer
    Public bottom As Integer
End Structure




Public Class GlassHelper
    <DllImport("dwmapi.dll", PreserveSig:=False)> Public Shared Sub DwmExtendFrameIntoClientArea(ByVal hWnd As IntPtr, ByRef pMarInset As MARGINS)
    End Sub
    <DllImport("dwmapi.dll", PreserveSig:=False)> Public Shared Function DwmIsCompositionEnabled() As Boolean
    End Function

    Public Shared Function ExtendGlassFrame(ByRef wido As Form1, newmargin As MARGINS)


        If DwmIsCompositionEnabled() = False Then
            Return False
            Debug.WriteLine("no dwm")
        End If
        Dim hwnd As IntPtr = wido.Handle
        If hwnd = 0 Then
            Throw New InvalidOperationException("Can't find a window")
        End If

        Debug.WriteLine("hope yo have handle" & wido.Handle.ToString)
        DwmExtendFrameIntoClientArea(wido.Handle, newmargin)
        Return True

    End Function
End Class


'        public static bool ExtendGlassFrame(Window window, Thickness margin)
'    {
'        If (!DwmIsCompositionEnabled()) Then
'            return false;

'        IntPtr hwnd = new WindowInteropHelper(window).Handle;
'        if (hwnd == IntPtr.Zero)
'            throw new InvalidOperationException(
'              "The Window must be shown before extending glass.");

'        // Set the background to transparent from both the WPF and Win32 perspectives
'        window.Background = Brushes.Transparent;
'        HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor =
'          Colors.Transparent;

'        MARGINS margins = new MARGINS(margin);
'        DwmExtendFrameIntoClientArea(hwnd, ref margins);
'        return true;
'    }



'End Class


'  [DllImport("dwmapi.dll", PreserveSig=false)]
'  static extern bool DwmIsCompositionEnabled();

'  public static bool ExtendGlassFrame(Window window, Thickness margin)
'  {
'        If (!DwmIsCompositionEnabled()) Then
'      return false;

'    IntPtr hwnd = new WindowInteropHelper(window).Handle;
'    if (hwnd == IntPtr.Zero)
'      throw new InvalidOperationException(
'        "The Window must be shown before extending glass.");

'    // Set the background to transparent from both the WPF and Win32 perspectives
'    window.Background = Brushes.Transparent;
'    HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor =
'      Colors.Transparent;

'    MARGINS margins = new MARGINS(margin);
'    DwmExtendFrameIntoClientArea(hwnd, ref margins);
'    return true;
'  }
'}
